<template>
  <div>
    <div ref="main" style="width: 550px; height: 240px"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { onMounted, ref, watch } from "vue";

const props = defineProps({
  data: {
    type:String,
    default: '1',
  },
  dataX: {
    type: Array,
    default: ["A1", "A2", "B1", "B2", "C1", "仓储"],
  },
});

onMounted(() => {
  watch(
    () => props.data,
    (newValue) => {
      console.log("新值：", newValue);
      Line();
    }
  );
  Line();
});
const main = ref(null);
function Line() {
  let myChart = echarts.getInstanceByDom(main.value);
  if (myChart == null) {
    myChart = echarts.init(main.value);
  }
  // var myChart = echarts.init(main.value);
  var option;
  const symbolSize = 0;
  const data = props.data;
  option = {
    title: {
      text: "能耗kwh",
    },
    tooltip: {
      formatter: function (params) {
        var data = params.data || [0, 0];
        return data[0].toFixed(2) + ", " + data[1].toFixed(2);
      },
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true,
    },
    xAxis: {
      data: props.dataX,
      type: "category",
      axisLine: { onZero: false },
    },
    yAxis: {
      min: 0,
      max: 1000,
      type: "value",
      axisLine: { onZero: false },
    },
    series: [
      {
        id: "a",
        type: "line",
        smooth: true,
        symbolSize: symbolSize,
        data: data,
      },
    ],
  };
  // var zr = myChart.getZr();
  // zr.on("click", function (params) {
  //   var pointInPixel = [params.offsetX, params.offsetY];
  //   var pointInGrid = myChart.convertFromPixel("grid", pointInPixel);
  //   if (myChart.containPixel("grid", pointInPixel)) {
  //     data.push(pointInGrid);
  //     myChart.setOption({
  //       series: [
  //         {
  //           id: "a",
  //           data: data,
  //         },
  //       ],
  //     });
  //   }
  // });
  // zr.on("mousemove", function (params) {
  //   var pointInPixel = [params.offsetX, params.offsetY];
  //   zr.setCursorStyle(
  //     myChart.containPixel("grid", pointInPixel) ? "copy" : "default"
  //   );
  // });

  option && myChart.setOption(option);
}
</script>

<style lang="scss" scoped></style>
